#include <stdio.h>
#include <stdlib.h>

// 静态表数据结构
typedef struct SqList{
    int *data;
    int length;
}SqList;

// 链表数据结构
typedef struct LNode
{
    int data;
    LNode *next;
}LNode, *LinkList;

// BulubuluSort
void BulubuluSort(SqList &L){
    int temp;
    for(int i = 0;i<L.length;i++){
        for(int j = 0;j<L.length-1-i;j++){
            if(L.data[j]>L.data[j+1]){
                temp = L.data[j];
                L.data[j] = L.data[j+1];
                L.data[j+1] = temp;
            }
        }
    }
}

void BulubuluSort2(LinkList &L){
    if(L == NULL)
        return;
    int temp;
    int num = 0;
    LNode *p = L->next;
    while (p != NULL)
    {
        num++;
        p = p->next;
    }

    p = L;
    for(int i = 0;i<num;i++){
        for(int j = 0;j<num-1-i;j++){
            p = p->next;
            if(p->data > p->next->data){
                temp = p->data;
                p->data = p->next->data;
                p->next->data = temp;
            }
        }
    }
}

// 尾插法
bool ListAppend(LinkList &L, int e)
{
    LNode *ptr = L;
    while (ptr->next != NULL)
        ptr = ptr->next;

    LNode *s = (LNode *)malloc(sizeof(LNode));
    if (s == NULL)
        return false;
    s->data = e;
    s->next = NULL;
    ptr->next = s;
    return true;
}

void PrintList(LinkList L)
{
    LNode *p = L->next;
    while (p)
    {
        printf("%d->", p->data);
        p = p->next;
    }
    printf("NULL\n");
}

//49 38 65 97 76 13 27 49 55 4

int main(){
    SqList L1;
    int data[10] = {49, 38, 65, 97, 76, 13, 27, 49, 55, 4};
    
    L1.data = data;
    L1.length = 10;
    for(int i = 0; i < L1.length; i++) {
        printf("%d ", L1.data[i]);
    }
    printf("\n");
    BulubuluSort(L1);
    for(int i = 0; i < L1.length; i++) {
        printf("%d ", L1.data[i]);
    }
    printf("\n");

    LinkList L2 = (LNode *)malloc(sizeof(LNode));
    L2->next = NULL;

    // 插入
    for(int i = 0; i < 10; i++) {
        ListAppend(L2, data[i]);
    }
    
    PrintList(L2);
    BulubuluSort2(L2);
    PrintList(L2);
    return 0;
}